package com.example.smartcity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.graphics.Color;
import android.media.Image;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.HttpURLConnection;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class TestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        //网络请求 ==》耗时操作----必须在子线程操作

        netWork();


    }


    private void netWork() {


        new Thread() {
            @Override
            public void run() {
                //构建http客户端  (c/s) client service
                OkHttpClient okHttpClient = new OkHttpClient();
                //请求体
                Request build = new Request
                        .Builder()
                        .url("http://192.168.0.101:8081/api/city/userinfo/rotation/lists?pageNum=1&pageSize=10&type=47")
                        .get()
                        .build();
//                try {
//                    Response execute = okHttpClient.newCall(build).execute();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
                okHttpClient.newCall(build).enqueue(new Callback() {

                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        //错误
                        e.getMessage();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(TestActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        });

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        //请求成功！
                        String json = response.body().string();
                        //构建 GSON
                        Gson gson = new Gson();
                        //解析得到 实体类
                        Bean1 bean1 = gson.fromJson(json, Bean1.class);
                        //切换线程
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindingView(bean1);
                            }
                        });
                    }
                });


            }
        }.start();
    }


    /**
     * 绑定UI 的数据
     *
     * @param bean1
     */
    private void bindingView(Bean1 bean1) {
        ViewPager viewById = findViewById(R.id.viewPage);
        viewById.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return bean1.getArrayList().size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return view == object;
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {

                View inflate = LayoutInflater.from(TestActivity.this).inflate(R.layout.item_layout, null, false);
                ImageView image = inflate.findViewById(R.id.image);
                Glide.with(TestActivity.this).load(
                        bean1.getArrayList().get(position).getImgUrl()).into(image);

                container.addView(image);
                return image;


            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView((View) object);
            }

            @Override
            public int getItemPosition(@NonNull Object object) {
                return super.getItemPosition(object);
            }
        });
    }
}